import {
    USER_LOGIN,
    INIT_USER,
    LOGIN_OUT
} from './mutations-types';

export default{
    state:{
        loginStatus:false,    //登录的状态
        token:null,        //token
        userInfo:{}        //用户的信息 用户的头像|用户的昵称
    },
    getters:{

    },
    mutations:{
        //设置
        //LOGIN是方法
        [USER_LOGIN](state,user){
			state.loginStatus = true;
			state.token = user.token;
			state.userInfo = user;
			//持久化存储==》本地存储
			localStorage.setItem('teaUserInfo',JSON.stringify(user));
		},
        //读取
		[INIT_USER]( state ){
			let userInfo = JSON.parse( localStorage.getItem('teaUserInfo') );
			if( userInfo ){
				state.loginStatus = true;
				state.token = userInfo.token;
				state.userInfo = userInfo;
			}
		},
        //退出登录  登录状态为false token为空 对象为空 删掉本地存储
        //退出登录
		[LOGIN_OUT]( state ){
			state.loginStatus = false;
			state.token = null;
			state.userInfo = {};
			localStorage.removeItem('teaUserInfo');
		}
    },
    actions: {
    },
    
}